package com.google.code.fluentjoiner;

import com.google.common.base.Preconditions;
import com.google.common.base.Supplier;
import org.apache.commons.lang.ObjectUtils;

public abstract class StringSupplierBuilder extends AbstractSupplierBuilder<String>
{
    public static final StringSupplierBuilder fromValue(final String value)
    {
        return new StringSupplierBuilder()
        {
            @Override
            protected String getSupplierResult()
            {
                return value;
            }
        };
    }

    public static final StringSupplierBuilder fromSupplierBuilder(final SupplierBuilder<?> supplierBuilder)
    {
        Preconditions.checkArgument(supplierBuilder != null, "Parameter supplierBuilder cannot be null.");
        final Supplier<?> originSupplier = supplierBuilder.getSupplier();
        return new StringSupplierBuilder()
        {
            @Override
            protected String getSupplierResult()
            {
                final Object originValue = originSupplier.get();
                return ObjectUtils.toString(originValue, null);
            }
        };
    }

    public SupplierBuilder<StringBuilder> toStringBuilder()
    {
        final Supplier<String> originSupplier = getSupplier();
        return new AbstractSupplierBuilder<StringBuilder>()
        {
            @Override
            protected StringBuilder getSupplierResult()
            {
                return new StringBuilder(originSupplier.get());
            }
        };
    }
}
